package com.gst.chat;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.Callback;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.hyphenate.helpdesk.easeui.widget.EaseChatInputMenu;
import com.hyphenate.helpdesk.easeui.widget.MessageList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.chat)
    Button chat;
    @Bind(R.id.fl_chat)
    FrameLayout flChat;
    @Bind(R.id.out)
    Button out;
    @Bind(R.id.btn_list)
    Button btnList;
    @Bind(R.id.message_list)
    MessageList messageList;

    /**
     * 传入fragment的参数
     */
    protected Bundle fragmentArgs;
    protected String toChatUsername;
    protected boolean showUserNick;
    protected EaseChatInputMenu inputMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }
//
//    @OnClick(R.id.chat)
//    public void onViewClicked() {
//        startChat();
//    }

    private void startChat() {
        if (ChatClient.getInstance().isLoggedInBefore()) {
            //已经登录，可以直接进入会话界面
            Intent intent = new IntentBuilder(MainActivity.this).
                    setServiceIMNumber("1760702095@qq.com") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                    .build();
            startActivity(intent);
        } else {
            //未登录，需要登录后，再进入会话界面
        }

    }

    @OnClick({R.id.chat, R.id.out,R.id.btn_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chat:
                startChat();
                break;
            case R.id.btn_list:
                openList();
                break;
            case R.id.out:
                //第一个参数为是否解绑推送的devicetoken
                ChatClient.getInstance().logout(true, new Callback() {
                    @Override
                    public void onSuccess() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                        startActivity(new Intent(MainActivity.this, LoginActivity.class));
                    }

                    @Override
                    public void onError(int i, String s) {

                    }

                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
                break;
        }
    }

    private void openList() {
        startChat();
    }
}
